<template>
	<view>
		<view class="Title">
			文字描述
		</view>
		<u--textarea v-model="value" placeholder="请描述事情的简要经过~"
			style="border: none;background-color: #202126;border-radius: 20rpx;margin: 0 30rpx;height: 215rpx;margin-bottom: 20rpx;"></u--textarea>
		<view class="Form">
			<view class="Item" style="border-bottom: 1rpx solid #d4d4d470;">
				<view class="Left">
					姓名<text>*</text>
				</view>
				<u-input class="Right" border="none"
					placeholder-style="text-align:right;font-size: 28rpx;color: #999999;" v-model="name"
					placeholder="请填写姓名"></u-input>
			</view>
			<view class="Item">
				<view class="Left">
					手机<text>*</text>
				</view>
				<u-input class="Right" border="none"
					placeholder-style="text-align:right;font-size: 28rpx;color: #999999;" v-model="mobile"
					placeholder="请填写手机号"></u-input>
			</view>
		</view>
		<view class="Title">
			上传图片
		</view>
		<view class="UPload">
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="3">
				<image src="/static/sy.png" style="width: 154rpx;height: 154rpx;"></image>
			</u-upload>
		</view>
		<view class="Sub" @click="Sub">
			立即提交
		</view>
	</view>
</template>

<script>
	import {
		apiBaseUrl
	} from '@/config/config.js'
	export default {
		data() {
			return {
				value: '',
				name: '',
				mobile: '',
				fileList1: [],
			};
		},
		methods: {
			Sub() {
				let Arr = []
				this.fileList1.forEach((A, B) => {
					Arr.push(A.url)
				})

				this.$api.FeedBack({
					truename: this.name,
					mobile: this.mobile,
					desc: this.value,
					image: Arr
				}, res => {
					if (res.status) {
						uni.showToast({
							title: '提交成功'
						})
					} else {
						this.$common.errorToShow(res.msg)
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},

			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: apiBaseUrl + 'api.html', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							'method': 'images.upload',
							'upfile': url
						},
						success: (res) => {
							//如果要路径在这里修改
							resolve(JSON.parse(res.data).data.image_id)
						}
					});
				})
			},
		}

	}
</script>

<style lang="scss" scoped>
	::v-deep .uni-input-input {
		text-align: right !important;
		color: #b9b9b9;
	}

	.Title {
		padding: 30rpx 20rpx;
		margin: 0 30rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #fff;
	}

	.Form {
		background-color: #202126;
		border-radius: 20rpx;
		margin: 0 30rpx;

		.Item {
			display: flex;
			align-items: center;
			margin: 0 30rpx;
			padding: 30rpx 20rpx;

			.Left {

				font-size: 28rpx;
				color: #fff;

				text {
					color: #F2D7AAFF;
				}
			}

			.Right {
				flex: 1;
				text-align: right;
			}
		}
	}

	.UPload {
		margin: 0 30rpx;
		padding: 30rpx 20rpx;
		background-color: #202126FF;
		border-radius: 20rpx;
	}

	.Sub {
		width: 629rpx;
		height: 75rpx;
		background: linear-gradient(180deg, #F2D7AA 0%, #DEBC86 100%);
		opacity: 1;
		border-radius: 10rpx;
		position: fixed;
		bottom: 94rpx;
		text-align: center;
		line-height: 75rpx;
		font-weight: bold;
		color: #181818;
		margin-left: 60rpx;
	}
</style>